package me;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yexy
 * @date 2020/6/30 11:01 PM
 * @description
 */
public class ChatServer {

    private static Message serverM;
    private static List<Socket> socketList = new ArrayList<>();
    private static String dataPath = "./";

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(9999);
            while(true){
                Socket socket = serverSocket.accept();
                socketList.add(socket);
                Thread thread = new Thread(new ServerThread(socket,socketList));
                thread.start();
            }



        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}


class ServerThread implements Runnable {

    Socket socket;
    private List<Socket> socketList;

    private String dataPath = "./";

    public ServerThread(Socket socket, List<Socket> socketList) {
        this.socket = socket;
        this.socketList = socketList;
    }

    @Override
    public void run() {
        try {
            boolean flag = true;

            ObjectInputStream objectInputStream = null;
            ObjectOutputStream objectOutputStream = null;
            BufferedInputStream bufferedInputStream = null;
            BufferedOutputStream bufferedOutputStream = null;

            objectInputStream = new ObjectInputStream(socket.getInputStream());
            objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            bufferedInputStream = new BufferedInputStream(socket.getInputStream());
//            PrintStream printStream = new PrintStream(socket.getOutputStream());

            while (flag) {
                Message m = (Message) objectInputStream.readObject();
                System.out.println("客户端IP:" + socket.getInetAddress() + ":" + socket.getPort() + ":" + m.getMessage());
                if (m.getType() == 1) {
                    String rMsg = socket.getInetAddress() + ":" + socket.getPort() +"发送的消息" + ":" +m.getMessage();
                    m.setMessage(rMsg);
                        for (Socket socketC : socketList) {
                            if (socketC.isConnected()) {
//                                if (socketC != socket) {
                                    PrintStream printStream = new PrintStream(socketC.getOutputStream());
                                    printStream.println(rMsg);
//                                }
                            }
                        }

//                    objectOutputStream.writeObject(m);
                } else if (m.getType() == 2) {
                    File file = new File(dataPath + m.getMessage());

                    String rMsg = socket.getInetAddress() + ":" + socket.getPort() +"上传的文件位置是" + ":" + file.getAbsolutePath();
                    for (Socket socketC : socketList) {
                        if (socketC.isConnected()) {
                            if (socketC.isConnected()) {
//                                if (socketC != socket) {
                                PrintStream printStream = new PrintStream(socketC.getOutputStream());
                                printStream.println(rMsg);
//                                }
                            }

                        }
                    }
                    m.setMessage(rMsg);
                    objectOutputStream.writeObject(m);
                    if (file.exists()) file.delete();
                    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
//                    byte[] bytes = new byte[m.getFileContent().length];
//                    bufferedOutputStream.write(bytes);
                    byte[] bs = new byte[1024];
                    int count = 0;
                    while ((count = bufferedInputStream.read(bs)) != -1) {
                        bufferedOutputStream.write(bs, 0, count);
                        bufferedOutputStream.flush();
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
